package org.hgvc.webapp.ets.spring.service;

import java.util.Date;
import java.util.List;

import org.hgvc.webapp.ets.bo.ActionBulletin;
import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.AmortiseSchema;
import org.hgvc.webapp.ets.bo.CreditTerm;
import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.bo.MembershipCategory;
import org.hgvc.webapp.ets.bo.PointCategory;
import org.hgvc.webapp.ets.bo.PointMembershipMap;
import org.hgvc.webapp.ets.bo.ReInstateFreeTrx;
import org.hgvc.webapp.ets.constant.ActionBulletinConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.SystemConstant;
import org.hgvc.webapp.ets.jsf.beans.member.MemberAgreementTransferBean;
import org.hgvc.webapp.ets.spring.dao.MembershipHbmDao;
import org.jocsb.base.bo.User;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;
import org.jocsb.base.util.SystemUtil;


public class MembershipServiceImpl implements MembershipService {
	private MembershipHbmDao dao;

	public void setDao(MembershipHbmDao dao) {
		this.dao = dao;
	}
	
	//PointCategory
	public List<PointCategory> getPointCategoryList()throws BaseException{
		try{
			return dao.getPointCategoryList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.point.category", new Object[]{}));
		}
	}
	
	public void createPointCategory(PointCategory cat,User createdUser)throws BaseException{
		try{
			
			List<PointCategory> list = dao.getPointCategoryListWithNameCode(cat.getName(), cat.getCode());
			if(list.size()>0){
				throw new BaseException(SpringUtils.getMessage("error.duplicate.point.category", new Object[]{}));
			}
			
			cat.setCreatedBy(createdUser);
			cat.setCreatedDate(new Date());
			cat.setIsDeleted(CommonConstant.NOT_DELETED);
			dao.save(cat);
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.point.category", new Object[]{}));
		}
	}
	
	public void editPointCategory(PointCategory cat,User createdUser)throws BaseException{
		try{
			
			List<PointCategory> list = dao.getPointCategoryListWithNameCode(cat.getName(), cat.getCode());
			if(list.size()>0){
				for(PointCategory pcat:list){
					if(!cat.getPointCatId().equalsIgnoreCase(pcat.getPointCatId())){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.point.category", new Object[]{}));
					}
				}
			}
			
			cat.setModifiedBy(createdUser);
			cat.setModifiedDate(new Date());
			dao.merge(cat);
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.edit.point.category", new Object[]{}));
		}
	}
	
	public void deletePointCategory(List<PointCategory> list,User createdUser)throws BaseException{
		try{
			for(PointCategory cat:list){
				if(cat.isMarkAsAction()){
					cat.setModifiedBy(createdUser);
					cat.setModifiedDate(new Date());
					cat.setIsDeleted(CommonConstant.IS_DELETED);
					dao.update(cat);
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.point.category", new Object[]{}));
		}
	}
	
	//Armotise Schema
	public List<AmortiseSchema> getAmortiseSchemaList()throws BaseException{
		try{
			return dao.getAmortiseSchemaList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.amortise.schema", new Object[]{}));
		}
	}
	
	public void createAmortiseSchema(AmortiseSchema sch,User createdUser)throws BaseException{
		try{
			
			List<AmortiseSchema> list = dao.getAmortiseSchemaListWithNameCode(sch.getName(), sch.getName());
			if(list.size()>0){
				throw new BaseException(SpringUtils.getMessage("error.duplicate.amortise.schema", new Object[]{}));
			}
			
			sch.setCreatedBy(createdUser);
			sch.setCreatedDate(new Date());
			sch.setIsDeleted(CommonConstant.NOT_DELETED);
			dao.save(sch);
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.amortise.schema", new Object[]{}));
		}
	}
	
	public void editAmortiseSchema(AmortiseSchema sch,User createdUser)throws BaseException{
		try{
			
			List<AmortiseSchema> list = dao.getAmortiseSchemaListWithNameCode(sch.getName(), sch.getName());
			if(list.size()>0){
				for(AmortiseSchema sc:list){
					if(!sch.getAmortiseSchId().equalsIgnoreCase(sc.getAmortiseSchId())){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.amortise.schema", new Object[]{}));
					}
				}
			}
			
			sch.setModifiedBy(createdUser);
			sch.setModifiedDate(new Date());
			dao.merge(sch);
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.edit.amortise.schema", new Object[]{}));
		}
	}
	
	public void deleteAmortiseSchema(List<AmortiseSchema> list,User createdUser)throws BaseException{
		try{
			for(AmortiseSchema sch:list){
				if(sch.isMarkAsAction()){
					sch.setModifiedBy(createdUser);
					sch.setModifiedDate(new Date());
					sch.setIsDeleted(CommonConstant.IS_DELETED);
					dao.update(sch);
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.amortise.schema", new Object[]{}));
		}
	}
	
	
	//Credit Term
	public List<CreditTerm> getCreditTermList()throws BaseException{
		try{
			return dao.getCreditTermList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.credit.term", new Object[]{}));
		}
	}
	
	public void createCreditTerm(CreditTerm term,User createdUser)throws BaseException{
		try{
			
			List<CreditTerm> list = dao.getCreditTermListWithCodeName(term.getCode(), term.getName());
			if(list.size()>0){
				throw new BaseException(SpringUtils.getMessage("error.duplicate.credit.term", new Object[]{}));
			}
			
			term.setCreatedBy(createdUser);
			term.setCreatedDate(new Date());
			term.setIsDeleted(CommonConstant.NOT_DELETED);
			dao.save(term);
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.credit.term", new Object[]{}));
		}
	}
	
	public void editCreditTerm(CreditTerm term,User createdUser)throws BaseException{
		try{
			
			List<CreditTerm> list = dao.getCreditTermListWithCodeName(term.getCode(), term.getName());
			if(list.size()>0){
				for(CreditTerm t:list){
					if(!t.getCreditTermId().equalsIgnoreCase(term.getCreditTermId())){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.credit.term", new Object[]{}));
					}
				}
			}
			
			term.setModifiedBy(createdUser);
			term.setModifiedDate(new Date());
			dao.merge(term);
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.edit.credit.term", new Object[]{}));
		}
	}
	
	public void deleteCreditTerm(List<CreditTerm> list,User createdUser)throws BaseException{
		try{
			for(CreditTerm term:list){
				if(term.isMarkAsAction()){
					term.setModifiedBy(createdUser);
					term.setModifiedDate(new Date());
					term.setIsDeleted(CommonConstant.IS_DELETED);
					dao.update(term);
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.credit.term", new Object[]{}));
		}
	}
	
	//membership category
	public List<MembershipCategory> getMembershipCategoryList()throws BaseException{
		try{
			return dao.getMembershipCategoryList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.membership.category", new Object[]{}));
		}
	}
	
	public void createMembershipCategory(MembershipCategory cat,User createdUser)throws BaseException{
		try{
			
			List<MembershipCategory> list = dao.getMembershipCategoryListWithCodeName(cat.getCode(), cat.getName());
			if(list.size()>0){
				throw new BaseException(SpringUtils.getMessage("error.duplicate.membership.category", new Object[]{}));
			}
			
			cat.setCreatedBy(createdUser);
			cat.setCreatedDate(new Date());
			cat.setIsDeleted(CommonConstant.NOT_DELETED);
			dao.save(cat);
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.membership.category", new Object[]{}));
		}
	}
	
	public void editMembershipCategory(MembershipCategory cat,User createdUser)throws BaseException{
		try{
			
			List<MembershipCategory> list = dao.getMembershipCategoryListWithCodeName(cat.getCode(), cat.getName());
			if(list.size()>0){
				for(MembershipCategory c:list){
					if(!c.getMembershipCatId().equalsIgnoreCase(cat.getMembershipCatId())){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.membership.category", new Object[]{}));
					}
				}
			}
			
			cat.setModifiedBy(createdUser);
			cat.setModifiedDate(new Date());
			dao.merge(cat);
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.edit.membership.category", new Object[]{}));
		}
	}
	
	public void deleteMembershipCategory(List<MembershipCategory> list,User createdUser)throws BaseException{
		try{
			for(MembershipCategory cat:list){
				if(cat.isMarkAsAction()){
					cat.setModifiedBy(createdUser);
					cat.setModifiedDate(new Date());
					cat.setIsDeleted(CommonConstant.IS_DELETED);
					dao.update(cat);
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.membership.category", new Object[]{}));
		}
	}
	
	
	//membership
	public List<Membership> getMembershipList()throws BaseException{
		try{
			return dao.getMembershipList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.membership", new Object[]{}));
		}
	}
	
	public void createMembership(Membership membership,User createdUser,List<PointMembershipMap> mapList)throws BaseException{
		try{
			
			List<Membership> list = dao.getMembershipListByCatIdCodeName(membership.getMembershipCategory().getMembershipCatId()
					, membership.getCode(), membership.getName());
			if(list.size()>0){
				throw new BaseException(SpringUtils.getMessage("error.duplicate.membership", new Object[]{}));
			}
			
			membership.setCreatedBy(createdUser);
			membership.setCreatedDate(new Date());
			membership.setIsDeleted(CommonConstant.NOT_DELETED);
			dao.save(membership);
			
			for(PointMembershipMap map:mapList){
				map.setMembership(membership);
				map.setCreatedBy(createdUser);
				map.setCreatedDate(new Date());
				map.setIsDeleted(CommonConstant.NOT_DELETED);
				dao.save(map);
			}
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.membership", new Object[]{}));
		}
	}
	
	public void editMembership(Membership membership,User createdUser,List<PointMembershipMap> mapList)throws BaseException{
		try{
			
			List<Membership> list = dao.getMembershipListByCatIdCodeName(membership.getMembershipCategory().getMembershipCatId()
					, membership.getCode(), membership.getName());
			if(list.size()>0){
				for(Membership m:list){
					if(!m.getMembershipId().equalsIgnoreCase(membership.getMembershipId())){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.membership", new Object[]{}));
					}
				}
			}
			
			membership.setModifiedBy(createdUser);
			membership.setModifiedDate(new Date());
			dao.merge(membership);
			
			for(PointMembershipMap map:mapList){
				map.setModifiedBy(createdUser);
				map.setModifiedDate(new Date());
				dao.merge(map);
			}
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.edit.membership", new Object[]{}));
		}
	}
	
	public void deleteMembership(List<Membership> list,User createdUser)throws BaseException{
		try{
			for(Membership membership:list){
				if(membership.isMarkAsAction()){
					membership.setModifiedBy(createdUser);
					membership.setModifiedDate(new Date());
					membership.setIsDeleted(CommonConstant.IS_DELETED);
					dao.update(membership);
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.membership", new Object[]{}));
		}
	}
	
	
	
	//Reinstate Type
	public List<ReInstateFreeTrx> getReInstateFreeTrxList()throws BaseException{
		try{
			return dao.getReInstateFreeTrxList();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.list.reinstate", new Object[]{}));
		}
	}
	
	public void createReInstateFreeTrx(ReInstateFreeTrx res,User createdUser)throws BaseException{
		try{
			
			List<ReInstateFreeTrx> list = dao.getReInstateFreeTrxListWithCodeName(res.getCode(), res.getName());
			if(list.size()>0){
				throw new BaseException(SpringUtils.getMessage("error.duplicate.reinstate", new Object[]{}));
			}
			
			res.setCreatedBy(createdUser);
			res.setCreatedDate(new Date());
			res.setIsDeleted(CommonConstant.NOT_DELETED);
			dao.save(res);
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.create.reinstate", new Object[]{}));
		}
	}
	
	public void editReInstateFreeTrx(ReInstateFreeTrx res,User createdUser)throws BaseException{
		try{
			
			List<ReInstateFreeTrx> list = dao.getReInstateFreeTrxListWithCodeName(res.getCode(), res.getName());
			if(list.size()>0){
				for(ReInstateFreeTrx t:list){
					if(!t.getReInstateFreeTrxId().equalsIgnoreCase(res.getReInstateFreeTrxId())){
						throw new BaseException(SpringUtils.getMessage("error.duplicate.reinstate", new Object[]{}));
					}
				}
			}
			
			res.setModifiedBy(createdUser);
			res.setModifiedDate(new Date());
			dao.merge(res);
			
		}catch(BaseException be){
			throw be;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.edit.reinstate", new Object[]{}));
		}
	}
	
	public void deleteReInstateFreeTrx(List<ReInstateFreeTrx> list,User createdUser)throws BaseException{
		try{
			for(ReInstateFreeTrx t:list){
				if(t.isMarkAsAction()){
					t.setModifiedBy(createdUser);
					t.setModifiedDate(new Date());
					t.setIsDeleted(CommonConstant.IS_DELETED);
					dao.update(t);
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.delete.reinstate", new Object[]{}));
		}
	}
	
	public List<PointMembershipMap> getPointMembershipMapList(Membership membership)throws BaseException{
		try{
			return dao.getPointMembershipMapList(membership.getMembershipId());
		}catch(Exception ex){
			ex.printStackTrace();
			throw new BaseException(SpringUtils.getMessage("error.get.membership.point", new Object[]{}));
		}
	}
}
